// pages/followEdit/followEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    readOnly:false,
    isBack:false,
    clientId:'',
    clientName:'',
    popup_show:false,
    time_popup_show:false,
    contact_popup_show:false,
    currentTimestamp:'',
    followTime:'',
    followType:'',
    workFlow:'',
    contactId:'',
    contactName:'',
  },

  chooseClient(){
    wx.navigateTo({
      url: '../chooseClient/chooseClient',
    })
  },
    /**选择时间 */
  chooseTime(e){
    let currentTimestamp = currentTimestamp = this.data.followTime?new Date(this.data.followTime).getTime():new Date().getTime();
    this.setData({
      time_popup_show:true,
      currentTimestamp:currentTimestamp
    })
  },
  /**日期选择确认 */
  timeConfirm(e){
    this.setData({
      followTime:api.formatDate(e.detail),
      time_popup_show:false
    })
  },
  timeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  
  /**选项取消 */
  onCancel(e){
    this.setData({
      popup_show:false,
      contact_popup_show:false
    })
  },
  /**选项切换 */
  onConfirm(event){
    let that =this;
    const { picker, value, index } = event.detail;

    switch(this.data.nowSelect){
      case 'followType':
         //选择跟进方式
         that.setData({
          followType:value,
          popup_show:false
        })
      break;
      case 'workFlow':
         //选择跟进状态
         that.setData({
          workFlow:value,
          popup_show:false
        })
      break;
      case 'contactName':
        console.log(value);
         //选择联系人
         that.setData({
          contactName:value.name,
          contactId:value.id,
          contact_popup_show:false
        })
      
    }
  },

  /**选项 */
  chooseSelect(e){
    let that = this;
    let type = e.currentTarget.dataset.type;
    switch(type){
      case 'followType':
         //选择跟进方式
      api.curlPost('?cla=clientAppClass&fun=params',{type:'followQuick'}).then((res)=>{
        that.setData({
          nowSelect:type,
          selectOption:res.data,
          popup_show:true,
        })
      })
      break;
      case 'workFlow':
         //选择跟进状态
      api.curlPost('?cla=clientAppClass&fun=params',{type:'workFlow'}).then((res)=>{
        that.setData({
          nowSelect:type,
          selectOption:res.data,
          popup_show:true,
        })
      })
      break;
      case 'contactName':
         //选择联系人
      api.curlPost('?cla=clientAppClass&fun=tabs',{khid:this.data.clientId,act:'staff'}).then((res)=>{
        that.setData({
          nowSelect:type,
          selectOption:res.data,
          contact_popup_show:true,
        })
      })
      break;
      
    }
  },

  /**提交 */
  commit(e){
    let para = e.detail.value;
    para.kehuStaffId=this.data.contactId;
    para.khid=this.data.clientId;
    para.targetName=this.data.clientName;
    para.workFlow=this.data.workFlow;
    api.curlPost('?cla=clientAppClass&fun=followEdit',para).then((res)=>{
      wx.navigateBack({
        delta:1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let khid = options.khid;
    let companyName = options.clientName;
    if (!api.isBlank(companyName)) {
      this.setData({
        // readOnly:true,
        clientName:companyName,
        clientId:khid,
        contactId:options.contactId,
        contactName:options.contactName,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isBack){
      this.setData({
        isBack:false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})